<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2024/5/9
 * Time: 17:48
 */

namespace app\zyhsapi\model;

use think\Log;
use XznsTools\cache\zyhs\CacheManageZyhs;

class ZyhsBlacklist extends BaseCacheZyhs
{
    protected $table = 'good_wechat.zyhs_blacklist';
    protected $cacheModel = 'zyhs.blacklis';

    public function getBlackList($type, $uid, $field = [], $limit = 0)
    {
        if (!$limit)
        {
            $this->pageLimit = $limit;
        }
        $data = $this->getListPage($uid, 'id', $field, $type);
        $data['data'] = $this->dataInit($data['data']);
        return $data;
    }

    /**
     * 获取用户 黑名单列表uid
     * @param int $atype 1=>平行宇宙
     * @param int $uid
     * @return array
     */
    public function getBlackUids(int $atype, int $uid)
    {
        $backList = $this->getBlackList($atype, $uid, ['b_uid'], -1);
        if ($backList['data'])
        {
            $bUids = array_column($backList['data'], 'b_uid');
            return $bUids;
        }

        return [];
    }

    /**
     * 添加黑名单
     * @param $type
     * @param $uid
     * @param $bUid
     * @return int|string
     */
    public function saveData($type, $uid, $bUid)
    {
        try
        {
            $id = $this->insertGetId(['uid' => $uid, 'type' => $type, 'b_uid' => $bUid]);
            $this->listDataAdd($uid, [$id => $id], $type);
        }
        catch (\Exception $e)
        {
            Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            $id = 0;
        }
        return $id;
    }

    /**
     * 删除黑名单
     * @param $type
     * @param $uid
     * @param $id
     * @param $data
     * @return bool
     */
    public function updateData($type, $uid, $id, $data)
    {
        try
        {
            $this->where(['id' => $id])->update($data);
            $this->listDataDel($uid, [$id], $type);
            CacheManageZyhs::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache();
        }
        catch (\Exception $e)
        {
            Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
        return true;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    public function dataInit(array $resData): array
    {
        foreach ($resData as $key => $itemData)
        {
            $resData[$key] = arrayKeyData2int($itemData, ['id', 'uid', 'b_uid', 'type', 'create_time', 'delete_time']);
        }
        return $resData;
    }

    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        if ($ids)
        {
            $where['id'] = ['in', $ids];
        } else
        {
            $where = ['uid' => $type[0], 'type' => $type[1], 'delete_time' => 0];
        }
        return $this->where($where)->column('*', 'id');
    }
}
